package org.paradise.dms.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.paradise.dms.pojo.MaintainReport;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;

import com.dheaven.framework.dao.BaseHibernateDao;
import com.dheaven.framework.dao.DaoException;

/**
 * 
 * Description: 报修管理DAO类 Copyright (c) 2008-2009 Neo. All Rights Reserved.
 * 
 * @version 1.0 Mar 18, 2009 9:42:43 PM 李萌（neolimeng@gmail.com）created
 */

@Service
public class MainReportDAOImpl extends BaseHibernateDao<MaintainReport> {
	private static Logger log = Logger.getLogger(MainReportDAOImpl.class);

	/**
	 * 
	 * Description: 增加一条维修记录
	 * 
	 * @Version1.0 Mar 19, 2009 5:01:27 PM 李萌（neolimeng@gmail.com）创建
	 * @param maintainreport
	 * @return
	 */
	public boolean insertMaintainReport(MaintainReport maintainreport) {
		try {
			this.create(maintainreport);
			return true;
		} catch (Exception e) {
			log.error("DMS_error_" + e);
			return false;
		}
	}

	/**
	 * 
	 * Description: 删除一条维修记录
	 * 
	 * @Version1.0 Mar 19, 2009 9:58:15 AM 李萌（neolimeng@gmail.com）创建
	 * @param maintainreportid
	 * @return
	 */
	public boolean deleteMaintainReport(int maintainreportid) {
		MaintainReport maintainreport = new MaintainReport();
		maintainreport.setMaintainreportid(maintainreportid);
		try {
			this.delete(maintainreport);
			return true;
		} catch (Exception e) {
			log.error("DMS_error_" + e);
			return false;
		}
	}

	/**
	 * 
	 * Description: 根据maintainreportid删除一条维修记录
	 * 
	 * @Version1.0 Mar 20, 2009 10:15:01 PM 郑旭（zhengxu2006@gmail.com）创建
	 * @param maintainreportid
	 * @return
	 */
	public boolean deleteMaintainReportByMaintainreportid(int maintainreportid) {

		// Query query =
		// this.createQuery("from maintainreport where maintainreportid = ?
		// ",maintainreportid);
		String hql = "update MaintainReport m set m.maintainreportind=0 where m.maintainreportid="
				+ maintainreportid;
		Query queryupdate = this.getSession().createQuery(hql);
		int n = queryupdate.executeUpdate();// n表示影响的行数
		return n > -1;

	}

	/**
	 * 
	 * Description: 更新一条维修记录
	 * 
	 * @Version1.0 Mar 19, 2009 5:03:09 PM 李萌（neolimeng@gmail.com）创建
	 * @param maintainreport
	 * @return
	 */
	public boolean updateMaintainReport(MaintainReport maintainreport) {
		try {
			this.update(maintainreport);
			return true;
		} catch (Exception e) {
			log.error("DMS_error_" + e);
			return false;
		}
	}

	/**
	 * 
	 * Description: 取得一条维修记录
	 * 
	 * @Version1.0 Mar 18, 2009 10:09:22 PM 李萌（neolimeng@gmail.com）创建
	 * @param maintainreportid
	 *            需要修改的维修记录的ID值
	 * @return
	 * @throws DaoException
	 */
	@SuppressWarnings("unchecked")
	public List<MaintainReport> getMaintainReport(int maintainreportid)
			throws DaoException {
		return this
				.find(
						"from MaintainReport where maintainreportid = ? and maintainreportind = 1",
						new Object[] { maintainreportid });
	}

	/**
	 * 
	 * Description: 取得所有维修记录
	 * 
	 * @Version1.0 Mar 18, 2009 10:10:03 PM 李萌（neolimeng@gmail.com）创建
	 * @return
	 * @throws DaoException
	 */

	@SuppressWarnings("unchecked")
	public List<MaintainReport> getMaintainReports() throws DaoException {
		return this.find("from MaintainReport where  maintainreportind = 1");
	}

	public int getAllRows() {
		return this.find("from MaintainReport where  maintainreportind = 1")
				.size();
	}

	@SuppressWarnings("unchecked")
	public List<MaintainReport> listAllMaintainReport(int pageSize, int startRow) {
		final int pageSize1 = pageSize;
		final int startRow1 = startRow;
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public List doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session
						.createQuery("from MaintainReport where  maintainreportind = 1");
				query.setFirstResult(startRow1);
				query.setMaxResults(pageSize1);
				return query.list();
			}
		});
	}

	public void setFinished(int mrid) {
		SQLQuery sqlQuery = (SQLQuery) this
				.getSession()
				.createSQLQuery(
						"update maintainreport set maintainreportstatusid=2 where maintainreportid=?")
				.setInteger(0, mrid);
		sqlQuery.executeUpdate();
	}
}
